package com.smrtcold.dev.utils;

import com.alibaba.fastjson.JSONObject;
import com.smrtcold.dev.dto.pwc.PWC;

import java.time.LocalDate;
import java.util.Set;

public class PWCUtil {

    public static PWC getPMconf(String mapping) {
        if (Utils.isNull(mapping)||mapping.length()<10) {
            mapping = "{\"type\":\"0\",\"pwc\":\"0.78\"}";
        }
        String Mkey = "M-" + LocalDate.now().getMonthValue();
        if (mapping.contains(Mkey)) {
            JSONObject jsonObject = JSONObject.parseObject(mapping);
            Set<String> keys = jsonObject.keySet();
            for (String key : keys) {
                if (key.contains(Mkey)) {
                    // 分割键中的月份部分 (如 "M-1,M-2,M-3" -> ["M-1", "M-2", "M-3"])
                    String[] monthKeys = key.split(",");
                    for (String str : monthKeys) {
                        if (str.equalsIgnoreCase(Mkey)) {
                            PWC pwc = Utils.GSON.fromJson(jsonObject.getString(key), PWC.class);
                            pwc.setType( jsonObject.getInteger("type"));
                            return pwc;
                        }
                    }
                }
            }
        }
        return Utils.GSON.fromJson(mapping, PWC.class);
    }


}
